package org.example.web.controller;

import org.example.biz.DepartmentBiz;
import org.example.oa.entity.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * 对部门增删改查的Controller
 */
@Controller("departmentController")
@RequestMapping("/department")//设置本控制器类的访问路径
public class DepartmentController {

    @Autowired
    private DepartmentBiz departmentBiz;

    //获取数据库部门列表
    @RequestMapping("/list")
    public String list(Map<String,Object> map){
        map.put("list", departmentBiz.getAll());
        //根据spring-web。xml视图解析器解析为/WEN-INF/pages/department_list.jsp
        return "department_list";
    }


    //打开添加部门的页面
    @RequestMapping("/to_add")
    public String toAdd(Map<String,Object> map){
        map.put("department", new Department());
        return "department_add";
    }
    //添加部门，重定向到list请求
    @RequestMapping("/add")
    public String add(Department department){
        departmentBiz.add(department);
        return "redirect:list";
    }


    //跳转到更新部门信息页面
    @RequestMapping(value = "/to_update", params = "sn")
    public String toUpdate(String sn, Map<String, Object> map){
        map.put("department", departmentBiz.get(sn));
        return "department_update";
    }
    //更新部门信息,重定向到list请求
    @RequestMapping("/update")
    public String update(Department department){
        departmentBiz.edit(department);
        return "redirect:list";
    }


    //删除部门,重定向到list请求
    @RequestMapping(value = "/remove", params = "sn")
    public String remove(String sn){
        departmentBiz.remove(sn);
        return "redirect:list";
    }
}
